﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;
using RegionInterface;

public class test
{
    public GUIModule Module;
    public test(GUIModule module)
    {
        Module = module;
        RegionStateManager.LoginStateChangeHandle += delegate(LoginState form, LoginState to)
        {
            if (to == LoginState.LoginEnd)
            {
                btn_Connect = "Disconnect";
            }
            else if (to == LoginState.LogoutEnd || to == LoginState.LoginError)
            {
                btn_Connect = "Connect";
            }
        };
        colorMaterial = Define.Mono.transform.Find("Plane").renderer.material;
    }
    public Rect[] rects = new Rect[]
    {
        new Rect(400, 10, 100, 25),
        new Rect(500, 10, 100, 25),
        new Rect(400, 40, 100, 25),
        new Rect(500, 40, 100, 25),
        new Rect(400, 70, 100, 25),
        new Rect(400, 100, 100, 25),
        new Rect(650, 50, 64, 64)
    };
    public string btn_Connect = "Connect";
    //public string txt_ip = "10.101.48.13";
    public string txt_ip = "127.0.0.1";
    public string txt_port = "80";
    public void OnGUI()
    {
        if (enable)
        {
            GUI.Label(rects[0], "Ip:");
            txt_ip = GUI.TextField(rects[1], txt_ip);
            GUI.Label(rects[2], "Port:");
            txt_port = GUI.TextField(rects[3], txt_port);
            if (GUI.Button(rects[4], btn_Connect))
            {
                if (string.Compare(btn_Connect, "Connect") == 0)
                {
                    btn_Connect = "Wait...";
                    testLogin();
                }
                if (string.Compare(btn_Connect, "Disconnect") == 0)
                {
                    AppInterface.NetworkModule.CloseServer(ServerType.RegionServer);
                    btn_Connect = "Connect";
                }
            }
            if (GUI.Button(rects[5], "Test"))
            {
                colorChangeStart = true;
                enable = false;
            }
        }
    }
    public void FixedUpdate()
    {
        colorChange();
    }
    private Color colorValue = Color.white;
    private bool colorChangeStart = false;
    private Material colorMaterial = null;
    private void colorChange()
    {
        if (colorChangeStart)
        {
            //colorValue.a -= .05f;
            colorValue.a -= .1f;
            if (colorValue.a <= 0)
            {
                colorValue.a = 0;
                colorChangeStart = false;
                WaitHandle.Start(.5f, delegate()
                {
                    AppInterface.AvatarModule.AddPlayer(true, new PlayerData(123, "小叮当", "p"));
                    Define.NoNetwork = true;
                    RegionStateManager.LoginStateChange(LoginState.LoginEnd);
                });
            }
            colorMaterial.color = colorValue;
        }
    }
    private bool enable = true;
    private void testLogin()
    {
        AssetHelp.CheckSocketPolicy(txt_ip, int.Parse(txt_port), delegate(bool re)
        {
            if (re)
            {
                AppInterface.NetworkModule.OpenServer(ServerType.RegionServer, txt_ip, int.Parse(txt_port), 1, "test001");
                int id = new System.Random((int)System.DateTime.Now.Ticks).Next(0, 99999999);
                AppInterface.NetworkModule.PlayerLogin(ServerType.RegionServer, id, "Player_" + id, "p");
            }
            else
            {
                btn_Connect = "Connect";
            }
        });
    }
}

